//
//  SmartControlInstallViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/4/12.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire
class SmartControlInstallViewController: BaseViewController {

    @IBOutlet var deviceNumTF: UITextField!
    
    @IBOutlet var installTimeTF: UITextField!
    
    
    @IBOutlet var installTimeBtn: UIButton!
    
    @IBOutlet var remarksTF: UITextField!
    
    
    @IBOutlet var submitBtn: UIButton!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.backgroundColor = UIColor.white
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        baseSetNavRightButtonWithTitle("已安装")
        
    }
    override func baseNavRightButtonPressed(_ button: UIButton) {
        let installListVC = SmartInstallListViewController()
        installListVC.title = "已安装列表"; self.navigationController?.pushViewController(installListVC, animated: true)
    }
    
    //网络请求
    func requestSmartcontrolInstall(installTime:String,deivceNum:String,remarks:String) -> Void {
        
        let param =
            ["install_time":installTime,
             "device_num":deivceNum,
             "uid":UserModel.shareUser.data?.uid ?? String(),
             "remarks":remarks,
             "sid":UserModel.shareUser.data?.storeId ?? String(),
             "price":"200"
             ]
        
        Alamofire.request(smartControlInstall, method:.post, parameters:param)
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = DefaultData()
                    basemodel = DefaultData.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
                        hud?.mode = MBProgressHUDMode.text
                        hud?.labelText = "提交成功"
                        hud?.show(animated: true, whileExecuting: {                        Thread.sleep(forTimeInterval: 0.8)
                        }, completionBlock: {                    self.navigationController?.popViewController(animated: true)
                        })
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
                
        }
    }
    
    
    @IBAction func installTimeBtnPress(_ sender: UIButton) {
        self.view.endEditing(true)
        BRDatePickerView.showDatePicker(withTitle: "安装时间", dateType: .dateAndTime, defaultSelValue:NSDate.currentDateString(), minDateStr:NSDate.currentDateString(), maxDateStr: "", isAutoSelect: true) { (timeStr) in
            self.installTimeTF.text = timeStr
        }
    }
    
    @IBAction func submitBtnPress(_ sender: UIButton) {
        
        if (installTimeTF.text == "") || (self.deviceNumTF.text == "") || (self.remarksTF.text == "") {
            MBProgressHUD.alertShow(in: self.view, message: "请填写完整再提交", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            return
        }
        
        
        requestSmartcontrolInstall(installTime: installTimeTF.text!, deivceNum: deviceNumTF.text!, remarks: remarksTF.text!)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
